/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.web.controller.api;


import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.*;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.common.dto.CurUserDto;
import com.medusa.gruul.integral.service.IIntegralDetailService;
import com.medusa.gruul.integral.web.model.dto.IntegralDetailDto;
import com.medusa.gruul.integral.web.model.param.api.IntegralDetailParam;
import com.medusa.gruul.integral.api.model.UserInfoVo;
import com.medusa.gruul.integral.web.model.vo.IntegralDetailVo;
import com.medusa.gruul.order.api.model.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * <p>
 * 积分明细表
 前端控制器
 * </p>
 *
 * @author xiaoq
 * @since 2019-11-19
 */
@RestController
@RequestMapping("/detail")
@Api(tags = "积分明细接口")
public class IntegralDetailController {

	@Autowired
	private IIntegralDetailService integralDetailService;

	/**
	 * 积分明细查看
	 *
	 * @param integralDetailParam 分页对象
	 * @return 分页返回的数据
	 */
	@GetMapping("/get")
	@ApiOperation(value = "积分明细获取")
	public Result<PageUtils<IntegralDetailVo>> getIntegralDetail(IntegralDetailParam integralDetailParam) {
		CurUserDto feignCurUser = CurUserUtil.getHttpCurUser();
		String userId = feignCurUser.getUserId();
		if (!StringUtil.isEmpty(userId)) {
			integralDetailParam.setUserId(userId);
		}
		PageUtils<IntegralDetailVo> pageUtils = new PageUtils(integralDetailService.selectIntegralDetail(integralDetailParam));
		return Result.ok(pageUtils);
	}


	/**
	 * 删除积分明细记录
	 * @param integralDetailId 积分明细id
	 *
	 * @return
	 */
	@DeleteMapping("delete")
	@ApiOperation(value = "删除积分明细记录,未对接扣除积分")
	public Result deleteDetail(Long integralDetailId) {
		integralDetailService.delDetail(integralDetailId);
		return Result.ok();
	}


	/**
	 * 用户每日首次分享 获得积分值
	 *
	 *
	 * @return 用户分享获得积分
	 */
	@PostMapping("save/share")
	@ApiOperation(value = "用户首次分享获得积分")
	@EscapeLogin
	public Result userShare() {
		CurUserDto feignCurUser = CurUserUtil.getHttpCurUser();
		String userId = feignCurUser.getUserId();
        String shopId = ShopContextHolder.getShopId();
        if (userId == null) {
			throw new ServiceException("数据错误", SystemCode.DATA_NOT_EXIST_CODE);
		}
		BigDecimal bigDecimal = integralDetailService.addIntegralDetail(shopId, userId);
		return Result.ok(bigDecimal);
	}


	/**
	 * 获取用户信息
	 * @return
	 */
	@PostMapping("get/userInfo")
	@ApiOperation(value = "用户信息")
	public Result<UserInfoVo> userInfo() {
		CurUserDto feignCurUser = CurUserUtil.getHttpCurUser();
		String userId = feignCurUser.getUserId();
		if (userId == null) {
			throw new ServiceException("数据错误", SystemCode.DATA_NOT_EXIST_CODE);
		}
		UserInfoVo userInfo = integralDetailService.getUserInfo(userId);
		return Result.ok(userInfo);
	}




	/*pc端客户管理操作*/
	//===========================================================================================================

	/**
	 * 积分明细获取
	 * @param integralDetailDto 查询条件
	 * @return 符合条件的数据
	 */
	@GetMapping("gain/detail")
	@ApiOperation(value = "pc端积分明细获取")
	public Result<PageUtils<IntegralDetailVo>> getUserIntegralDetail(IntegralDetailDto integralDetailDto) {
		PageUtils<IntegralDetailVo> pageUtils = new PageUtils(integralDetailService.getIntegralDetail(integralDetailDto));
		return Result.ok(pageUtils);
	}


	/**
	 * 批量充值或扣除积分
	 * @param userIds   用戶ids
	 * @param integral  变化的积分
	 * @param isDeduct  是否扣除
	 * @return
	 */
	@PutMapping("change")
	@ApiOperation(value = "商家给用户充值或扣除积分")
	public Result integralChange(@RequestBody String[] userIds,
			@ApiParam(value = "integral", required = true) BigDecimal integral,
			@RequestParam(value = "isDeduct") Boolean isDeduct) {
		if (isDeduct) {
			//批量扣除
			integralDetailService.deductIntegral(Arrays.asList(userIds), integral);
		} else {
			//批量充值
			integralDetailService.rechargeIntegral(Arrays.asList(userIds), integral);
		}
		return Result.ok();
	}


	@PutMapping("posta")
	@ApiOperation(value = "订单订阅测试  -- 未使用")
	public Result interg(OrderVo orderVo) {
		integralDetailService.orderFinish(orderVo);
		return Result.ok();
	}


	/**
	 *  商家给用户充值积分
	 *
	 * @param integral 充值的积分
	 * @return
	 */
	@PutMapping("recharge")
	@ApiOperation(value = "商家给用户充值积分")
	public Result shopRecharge(@ApiParam(value = "充值积分值", required = true) BigDecimal integral,
			@ApiParam(value = "用户id", required = true) Long userId) {
		return Result.ok();

	}


	/**
	 * 商家扣除用户积分
	 *
	 * @param integral 扣除积分值
	 * @return
	 */
	@PutMapping("deduct")
	@ApiOperation(value = "商家扣除用户积分")
	public Result shopDeduct(@ApiParam(value = "扣除积分值", required = true) BigDecimal integral,
			@ApiParam(value = "用户id", required = true) Long userId) {
		return Result.ok();
	}


}
